﻿@{
    ViewBag.Title = "采集网络文章";
}

@section styles{
    <style>
        .alter {
            height: 5px;
        }
    </style>
}
<div ms-controller="collection">
    <div class="am-cf am-padding">
        <div class="am-fl am-cf">
            <strong class="am-text-primary am-text-lg">文章采集</strong> /
            <small>collection</small>
        </div>
    </div>
    <form action="/Collection/PrevContent" id="ffff" method="post" target="_blank">
        <div class="am-g">
            <div class="am-u-md-5">
                <input type="text" placeholder="列表页面url" class="am-form-field"
                       name="url"
                       ms-duplex="list_url" />
                <p></p>
                <input type="text" placeholder="el" class="am-form-field"
                       name="url"
                       ms-duplex="list_url_el" />
                <p>
                    <button type="button" data-ajax ms-click="get_url_list">获取列表</button>
                </p>
                <textarea rows="10" style="width:100%" ms-duplex="url_list"></textarea>
                <p>
                    <button type="button" data-ajax ms-click="multi_collect">批量采集</button>
                </p>
            </div>
            <div class="am-u-md-6">
                <div class="am-g">
                    <div class="am-u-sm-12 am-u-md-12">
                        <input type="text" placeholder="标题网址" class="am-form-field"
                               name="url"
                               ms-duplex="collection_url" />
                    </div>
                </div>
                <p class="alter">
                    {{title}}
                </p>
                <div class="am-g">
                    <div class="am-u-sm-12 am-u-md-11">
                        <input type="text" placeholder="标题dom元素" class="am-form-field" ms-duplex="title_dom" />
                    </div>
                    <div class="am-u-sm-12 am-u-md-1">
                        <button type="button" class="am-btn am-btn-warning" ms-click="test_get_title" data-ajax>测试</button>
                    </div>
                </div>
                <p class="alter">
                    &nbsp;
                </p>
                <div class="am-g">
                    <div class="am-u-sm-12 am-u-md-5">
                        <input type="text" placeholder="详细内容dom元素，例如：//div[(at符)class='Post-body']"
                               name="detaildom"
                               class="am-form-field" ms-duplex="content_dom" />
                    </div>
                    <div class="am-u-sm-12 am-u-md-5">
                        <input type="text" placeholder="连续采集页数 默认是25" class="am-form-field"
                               name=""
                               ms-duplex="pages" />
                        <input type="hidden" name="IsGetDetail" value="true" />
                    </div>
                    <div class="am-u-sm-12 am-u-md-2">
                        <button type="button" class="am-btn am-btn-warning"
                                ms-click="test_get_detail">
                            测试
                        </button>
                    </div>
                </div>
            </div>
            <div class="am-u-md-1"></div>
        </div>
    </form>

    <p class="alter">
    </p>
    <div class="am-g">
        <div class="am-u-sm-12 am-u-md-1">
            <button type="button" class="am-btn am-btn-warning" ms-click="save_post" data-ajax>采集</button>
        </div>
        <p class="alter" ms-if="last_post_id">
            文章编号：{{last_post_id}} <a ms-attr-href="/post/edit/{{last_post_id}}" target="_blank">编辑</a>
        </p>
    </div>
</div>

@section scripts{
    <script>
        var vm = avalon.define({
            $id: 'collection',
            collection_url: '',
            title_dom: '',
            title: '',
            test_get_title: function () {
                $.post('title', { url: vm.collection_url, titleDom: vm.title_dom }).success(function (data) {
                    vm.title = data.title;
                }).error(function (data) {
                    alert(data);
                });
            },
            content_dom: '',
            pages: '',
            detail: '',
            last_post_id: '',
            test_get_detail: function () {
                if (vm.collection_url && vm.content_dom) {
                    $('#ffff').submit();
                } else {
                    alert('网址和dom必填')
                }
            },
            save_post: function (url) {
                $.post('SavePost', {
                    url: url || vm.collection_url,
                    titleDom: vm.title_dom,
                    detailDom: vm.content_dom,
                    detail: vm.detail,
                    pages: vm.pages
                }).success(function (id) {
                    vm.last_post_id = id;
                }).error(function () {
                    error_msg('error');
                });
            },
            list_url: '',
            list_url_el: '',
            url_list: '',
            list_total: 1,
            get_url_list: function () {
                $.post('GetUrlList', {
                    url: vm.list_url,
                    el: vm.list_url_el,
                    total: vm.list_total
                }).success(function (data) {
                    vm.url_list = data.join('\r\n');
                });
            },
            multi_collect: function () {
                var arr = vm.url_list.split('\n');

                $.each(arr, function (index, d) {
                    vm.save_post($.trim(d));
                });
            }
        });

    </script>
}